package com.platform.api;

import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.CategoryVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiCategoryService;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@Api(tags = "栏目")
@RestController
@RequestMapping("/api/catalog")
public class ApiCatalogController extends ApiBaseAction {
    @Autowired
    private ApiCategoryService categoryService;

    @ApiOperation(value = "分页获取L2分类列表")
    @IgnoreAuth
    @RequestMapping(value = "queryL2List", method = {RequestMethod.GET, RequestMethod.POST})
    public Object queryL2List(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "size", defaultValue = "10") Integer size) {

        //查询L2等级的分类
        Map<String, Object> params = new HashMap<>();
        params.put("level", "L2");
        params.put("isShow", 1);
        params.put("page", page);
        params.put("limit", size);
        params.put("sidx", "id");
        params.put("order", "asc");
        Query query = new Query(params);

        List<CategoryVo> categoryVos = categoryService.queryList(query);

        int total = categoryService.queryTotal(params);

        ApiPageUtils pageUtil = new ApiPageUtils(categoryVos, total, query.getLimit(), query.getPage());
        return toResponsSuccess(pageUtil);
    }


    /**
     * 获取分类栏目数据
     */
    @ApiOperation(value = "获取分类栏目数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = false)})
    @IgnoreAuth
    @RequestMapping(value = "index", method = {RequestMethod.GET, RequestMethod.POST})
    public Object index(Integer id) {
        Map<String, Object> resultObj = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("sidx", "sort_order");
        params.put("order", "asc");
        params.put("parent_id", 0);

        //查询列表数据
        List<CategoryVo> list = categoryService.queryList(params);

        // 当前分类
        CategoryVo currentCategory = null;
        if (null != id) {
            currentCategory = categoryService.queryObject(id);
        }

        if (null == currentCategory && null != list && list.size() != 0) {
            currentCategory = list.get(0);
        }

//        else {
//            currentCategory = new CategoryVo();
//        }

        //获取子分类数据
        if (null != currentCategory && null != currentCategory.getId()) {
            params.put("parent_id", currentCategory.getId());
            currentCategory.setSubCategoryList(categoryService.queryList(params));
        }


        resultObj.put("categoryList", list);//类别列表
        resultObj.put("currentCategory", currentCategory);//当前类别

        return toResponsSuccess(resultObj);
    }

    /**
     */
    @ApiOperation(value = "分类目录当前分类数据接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = false)})
    @IgnoreAuth
    @RequestMapping(value = "current", method = {RequestMethod.GET, RequestMethod.POST})
    public Object current(Integer id) {
        Map<String, Object> resultObj = new HashMap();
        Map params = new HashMap();
        params.put("parent_id", 0);
        CategoryVo currentCategory = null;
        if (null != id) {
            currentCategory = categoryService.queryObject(id);
        }
        //获取子分类数据
        if (null != currentCategory && null != currentCategory.getId()) {
            params.put("parent_id", currentCategory.getId());
            currentCategory.setSubCategoryList(categoryService.queryList(params));
        }
        resultObj.put("currentCategory", currentCategory);
        return toResponsSuccess(resultObj);
    }
}